package com.linjian.togetherbirthday.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.activity.SplashActivity;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.view.CustomDatePicker;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.Map;

public class BirthSetFragment extends BaseFragment implements View.OnClickListener, CustomDatePicker.ResultHandler {

    private SplashActivity splashActivity;
    private View mView;
    private CustomDatePicker customDatePicker;
    private Button bt_go;

    private String timeData = null;

    public static BirthSetFragment newInstance(String info) {
        BirthSetFragment birthSetFragment = new BirthSetFragment();
        Bundle bundle = new Bundle();
        bundle.putString("info",info);
        birthSetFragment.setArguments(bundle);
        return birthSetFragment;
    }

    @Override
    protected int setView() {
        return R.layout.fragmnet_birth;
    }

    @Override
    protected void init(View view) {
        mView = view;
        splashActivity = (SplashActivity) getActivity();
        customDatePicker = view.findViewById(R.id.cus);
        customDatePicker.show("1995-01-01");
        bt_go = view.findViewById(R.id.bt_go_birth);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        setListener();
    }
    private void setListener() {
        bt_go.setOnClickListener(this);
        customDatePicker.callBackTimeListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_go_birth:
                if (customDatePicker.getTime() == null){
                    TastyToast.makeText(getActivity(),"请选择生日",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else {
                    Map<String,Object> map = new Gson().fromJson(getArguments().getString("info"),Map.class);
                    map.put("birthday",customDatePicker.getTime());
                    splashActivity.replaceFragment("HeightSetFragment",new Gson().toJson(map));
                }

                break;
        }
    }

    @Override
    public void handle(String time) {
        Log.i("BirthFragment",time);
        timeData = time;
    }
}
